﻿using System;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
//Also added reference to EntityFramework.dll (EF 4.1)
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using PlanoAula;
using PlanoAula.BD;

namespace CfTest
{
    class Program
    {
        
        public static void Main(string[] args)
        {
            string file = @"c:\temp\Students.sdf";
            if (File.Exists(file))
                File.Delete(file);
            Database.SetInitializer<StudentContext>(new DropCreateDatabaseIfModelChanges<StudentContext>());
            string sql = StudentContext.CreateDatabaseScript();
            Console.WriteLine(
                   "SQL: \n\t {0}",
                   sql);
            StudentContext.GenerateRuntimeEDM(@"C:\Users\u031220\documents\visual studio 2010\Projects\TesteSQLCE\TesteSQLCE\Model.edmx");
            using (var db = new StudentContext())
            {
                
                db.DiaNaoUtil.Add(new DiaNaoUtil { Data = DateTime.Today, Descricao = "teste", Nome = "nome teste"});

                var student = new Student { Name = "Erik", Photo = ConvertImageToByteArray(@"C:\Users\u031220\Desktop\my_Logo4 - Cópia.png"), StudentId = 1 };
                student.LongText = new string('x', 6666);
                db.Students.Add(student);
                int recordsAffected = db.SaveChanges();

                Console.WriteLine(
                    "Saved {0} entities to the database, press any key to exit.",
                    recordsAffected);

                
            }

        }

        private static byte[] ConvertImageToByteArray(string fileName)
        {
            Bitmap bitMap = new Bitmap(fileName);
            ImageFormat bmpFormat = bitMap.RawFormat;
            var imageToConvert = Image.FromFile(fileName);
            using (MemoryStream ms = new MemoryStream())
            {
                imageToConvert.Save(ms, bmpFormat);
                return ms.ToArray();
            }
        }
    }


    public class Student
    {
        public int StudentId { get; set; }
        public string Name { get; set; }

        // Required to force Code First to create a ntext column, not a nvarchar(n)
        [Column(TypeName = "ntext")]
        public string LongText { get; set; }

        // Required to force Code First to create an image column, not a binary(n)
        [Column(TypeName = "image")]
        public virtual byte[] Photo { get; set; }
    }

    public class StudentContext : dbDataContext
    {
        
        protected override bool ShouldValidateEntity(System.Data.Entity.Infrastructure.DbEntityEntry entityEntry)
        {
            // Required to prevent bug - http://stackoverflow.com/questions/5737733
            if (entityEntry.Entity is Student)
            {
                return false;
            }
            return base.ShouldValidateEntity(entityEntry);
        }
        public DbSet<Student> Students { get; set; }
    }

}
